<script setup lang="ts">
// 导入所需模块
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
// 导入侧边栏状态管理
import { useSidebarStore } from '@/store/slidebar';
// 导入菜单数据
import { menuData } from './menu';

// 获取当前路由信息
const route = useRoute();
// 计算当前激活的路由路径
const onRoutes = computed(() => {
  return route.path;
});
// 使用侧边栏状态管理
const slidebar = useSidebarStore();
</script>

<template>
  <div class="slidebar">
    <!-- Element Plus 菜单组件 -->
    <el-menu
      :default-active="onRoutes"
      class="slidebar-el-menu"
      :collapse="slidebar.collapse"
      :background-color="slidebar.bgColor"
      :text-color="slidebar.textColor"
      router
    >
      <!-- 遍历菜单数据 -->
      <template v-for="item in menuData">
        <!-- 判断是否存在子菜单 -->
        <template v-if="item.children">
          <!-- 渲染含子菜单的菜单项 -->
          <el-sub-menu :index="item.index" :key="item.index" v-permiss="item.id">
            <template #title>
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <!-- 遍历子菜单 -->
            <template v-for="subItem in item.children">
              <!-- 判断是否存在三级菜单 -->
              <el-sub-menu
                v-if="subItem.children"
                :index="subItem.index"
                :key="subItem.index"
                v-permiss="item.id"
              >
                <template #title>{{ subItem.title }}</template>
                <!-- 渲染三级菜单项 -->
                <el-menu-item
                  v-for="(threeItem, i) in subItem.children"
                  :key="i"
                  :index="threeItem.index"
                >
                  {{ threeItem.title }}
                </el-menu-item>
              </el-sub-menu>
              <!-- 渲染二级菜单项 -->
              <el-menu-item v-else :index="subItem.index" v-permiss="item.id">
                {{ subItem.title }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 渲染无子菜单的菜单项 -->
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index" v-permiss="item.id">
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<style scoped>
/* 侧边栏样式 */
.slidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
/* 隐藏滚动条 */
.slidebar::-webkit-scrollbar {
  width: 0;
}

/* 展开状态下菜单宽度 */
.slidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
/* 菜单最小高度 */
.slidebar-el-menu {
  min-height: 100%;
}
</style>